package models;

import java.util.*;
import javax.persistence.*;

import play.Logger;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;

/**
 * User entity managed by Ebean
 */
@Entity
@Table(name="Payment")
public class Payment extends Model {

	@Id
	public Long id;

	public String seat_no;
	public String email;
	public String credit_card_no;
	public Date paid_time;
	public int status; // 0:Open 1:RequestedRefund 2:ApprovedRefund 3:RejectedRefund 4:LoyaltyPointsPurchase

	@ManyToOne
	public MovieSession session;

	@ManyToOne
	public User user;

    // -- Queries
    public static Model.Finder<Long,Payment> find = new Model.Finder(Long.class, Payment.class);

    public Payment(Long session, String seat, String email, String creditCard, String username, int status) {
		this.seat_no = seat;
		this.session = MovieSession.find.byId(session);
		this.email = email;
		this.credit_card_no = creditCard;
		this.paid_time = new Date();
		this.status = status;
		if(username == null){
			this.user = null;
		} else {
			this.user = User.findByUser(username);
		}
    }
	
	public static Payment findByID(Long id) {
		return find.where().eq("id",id).findUnique();		
	}	
	
	public static List<Payment> findByStatus (String language, int status) {
		return find.fetch("session.movie.infos").where().eq("locale", language).eq("status",status).findList();
	}

	public static List<Payment> findByUsernameAndStatus (String username, String language, int status) {
		return find.fetch("session.movie.infos").where().eq("locale", language).eq("user_username", username).eq("status",status).findList();
	}
	
	public static List<Payment> findUserAllActivePayment (String username, String language) {
		return find.fetch("session.movie.infos").where().ne("status",2).eq("locale", language).eq("user_username", username).findList();
	}

	public static boolean isTicketSold(Long session, String seat) {
		return (find.fetch("session").where().eq("seat_no", seat).eq("session_id", session).ne("status",1).ne("status",2).ne("status",3).findRowCount() > 0);
	}

	public static List<Payment> findBySession(Long session) {
		return find.fetch("session").where().eq("session_id", session).ne("status",1).ne("status",2).ne("status",3).findList();
	}
	
	public static boolean isMovieSessionPurchasedByAnyone(Long session){
		return (find.where().eq("session_id", session).ne("status",1).ne("status",2).ne("status",3).findRowCount() > 0);
	}
	
	public static boolean isPaymentOwner(String username, Long id) {
		return (find.fetch("session").where().eq("user_username",username).eq("id", id).findRowCount() == 1);
	}
	
	public static Payment findByUsernameSessionAndSeat(String username, Long session, String seat) {
		return find.where().eq("user_username",username).eq("session_id", session).eq("seat_no",seat).findUnique();
	}
	
	public static int findSalesByHouseId(int house_id) {
		return find.fetch("session").where().eq("house_id",house_id).findList().size();
	}
	
	public static boolean isPaymentForSeatExists(int house_id, String seat) {
		return (find.fetch("session").where().eq("house_id",house_id).eq("seat_no",seat).ne("status",1).ne("status",2).ne("status",3).gt("showtime", new Date()).findList().size() > 0);	
	}
	
	public static int findSalesByYearAndMonth(int year, int month) {
		return find.fetch("session").where("(DATEPART(Year, showtime)=" + year + ") AND (DATEPART(Month, showtime) = " + month + ")").findList().size();	
	}

}

